package
{
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.primitives.Plane;
	import alternativa.engine3d.events.MouseEvent3D;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.types.Texture;
	import alternativa.engine3d.core.Object3D;
	import alternativa.utils.MathUtils;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Tableau extends Mesh
	{
		[Embed(source="images/tableaublanc.jpg")] private static const bmpTableau:Class;
		private var tableauTexture:Texture = new Texture(new bmpTableau().bitmapData, "tableau");
		[Embed(source="images/brosse.jpg")] private static const bmpBrosse:Class;
		private var brosseTexture:Texture = new Texture(new bmpBrosse().bitmapData, "brosse");
		
		
		private var hauteur:int;
        private var largeur:int;
        private var longueur:int;
		
		private var brosse:Mesh;
		private var couleur:Mesh;
		private var couleurChoisie:Number;
        
		public function Tableau(name:String=null)
		{
			super(name);
			hauteur = 200;
            longueur = 300;
            largeur = 10;
            couleurChoisie=0x000000;
            
	      	createVertex(0,0,0,1);
	        createVertex(0,0,hauteur,2);
	        createVertex(largeur,0,0,3);
	        createVertex(largeur,0,hauteur,4);    
	        createVertex(largeur,longueur,0,5);
	        createVertex(largeur,longueur,hauteur,6);
	        createVertex(0,longueur,0,7);
	        createVertex(0,longueur,hauteur,8);
	            
	        createFace([3,5,6,4],"Fr");
	        setUVsToFace(new Point(0, 0), new Point(1, 0), new Point(1, 1), "Fr");
	        createFace([1,2,8,7],"Fl");
	        createFace([1,3,4,2],"Ff");
	        createFace([8,6,5,7],"Fb");
	        createFace([2,4,6,8],"Ft");
	        createFace([7,5,3,1],"Fs");
	        
	        createSurface(["Fr"], "On peut tagger");
			createSurface(["Ff","Fl","Ft","Fs","Fb"], "Le reste");
            
			ajouterBrosse();
			ajouterCouleur();
			
			
			setMaterialToSurface(new TextureMaterial(tableauTexture, 1, true, true, "normal", -1, 0x000000, 3),"On peut tagger");
			setMaterialToSurface(new FillMaterial(0xCCCCCC,1,"normal",1,0xCCCCCC),"Le reste");
			
            addEventListener(MouseEvent3D.MOUSE_DOWN,onMouseDown);
		}
		
		public function ajouterBrosse():void{
			brosse=new Plane(20,20,1,1);
			brosse.rotationX=MathUtils.toRadian(90);
			brosse.rotationZ=MathUtils.toRadian(90);
			brosse.x=10.1;
			brosse.y=10;
			brosse.z=hauteur-10;
			var textureB:TextureMaterial=new TextureMaterial(brosseTexture, 1, true, true, "normal", -1, 0x000000, 3)
			brosse.setMaterialToSurface(textureB,"front");
			brosse.addEventListener(MouseEvent3D.CLICK,effaceTout);
			addChild(brosse);
		}
		public function ajouterCouleur():void{
			couleur=new Plane(20,20,1,1);
			couleur.rotationX=MathUtils.toRadian(90);
			couleur.rotationZ=MathUtils.toRadian(90);
			couleur.x=10.1;
			couleur.y=10;
			couleur.z=hauteur-30;
			couleur.setMaterialToSurface(new FillMaterial(0x000000,1,"normal",1,0x000000),"front");
			couleur.addEventListener(MouseEvent3D.CLICK,changeCouleur);
			addChild(couleur);
		}
		
		
		public function onMouseDown(me:MouseEvent3D):void{
			addEventListener(MouseEvent3D.MOUSE_MOVE,dessiner);
			addEventListener(MouseEvent3D.MOUSE_UP,stopDessin); 
		}
		
		public function dessiner(me:MouseEvent3D):void{	
			if(me.ctrlKey){
				var object_pixel_x:Number=longueur*me.u;
				var object_pixel_y:Number=hauteur*me.v;
				tableauTexture.bitmapData.fillRect(new Rectangle(object_pixel_x-2,hauteur-object_pixel_y-2, 4, 4), couleurChoisie);
			}
		}
		public function stopDessin(me:MouseEvent3D):void{	
			removeEventListener(MouseEvent3D.MOUSE_MOVE,dessiner);
		}
		
		public function effaceTout(me:MouseEvent3D):void{
			tableauTexture.bitmapData.fillRect(new Rectangle(0,0,longueur,hauteur), 0xFFFFFFFF);
		}
		public function changeCouleur(me:MouseEvent3D):void{
			switch(couleurChoisie){
				case 0x000000:
					couleurChoisie=0xFF0000;
					couleur.setMaterialToSurface(new FillMaterial(0xFF0000,1,"normal",1,0x000000),"front");
					break;
				case 0xFF0000:
					couleurChoisie=0x00FF00;
					couleur.setMaterialToSurface(new FillMaterial(0x00FF00,1,"normal",1,0x000000),"front");
					break;
				case 0x00FF00:
					couleurChoisie=0x0000FF;
					couleur.setMaterialToSurface(new FillMaterial(0x0000FF,1,"normal",1,0x000000),"front");
					break;
				case 0x0000FF:
					couleurChoisie=0x000000;
					couleur.setMaterialToSurface(new FillMaterial(0x000000,1,"normal",1,0x000000),"front");
					break;
			}
		}
	}
}